package com.itlong.cloud.controller.home;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyUserCacheVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.propertyapp.IPropertyAppUserInfoServiceClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业APP首页版块住户审核模块控制器
 * </desc>
 *
 * @create 2019/12/30
 */
@RestController
@RequestMapping("/home")
public class PropertyAppHouseholdCheckController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyAppHouseholdCheckController.class);

    @Autowired
    IPropertyAppUserInfoServiceClient iPropertyAppUserInfoServiceClient;

    /**
     * <desc>
     *      获取住户审核列表。
     * <desc/>
     *
     * @param projectId  项目编号
     * @return
     * @author Juguang.S
     * @createDate 2019-12-30
     */
    @RequestMapping(value = "/getHoldAuditList", method = RequestMethod.POST)
    @ResponseBody
    public Object getHoldAuditList(String projectId) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();

            if (StringUtils.isBlank(projectId)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), iPropertyAppUserInfoServiceClient.getHoldAuditList(projectId));
        } catch (Exception e) {
            throw new DataAccessException("【物业APP_首页版块_住户审核】获取住户审核列表失败", e);
        }
    }

    /**
     * <desc>
     *      住户审核通过。
     * <desc/>
     *
     * @param projectId  项目编号
     * @param auditId    审核通过信息ID
     * @param type    1审核通过 2审核拒绝
     * @return
     * @author Juguang.S
     * @createDate 2019-12-30
     */
    @RequestMapping(value = "/agreeHoldAudit", method = RequestMethod.POST)
    @ResponseBody
    public Object agreeHoldAudit(String projectId,String auditId,String type) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
            auditId = request.getAttribute("auditId") == null ? null : request.getAttribute("auditId").toString();
            type = request.getAttribute("type") == null ? null : request.getAttribute("type").toString();

            if (StringUtils.isBlank(projectId) || StringUtils.isBlank(auditId) || StringUtils.isBlank(type)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Map<String,String> map = new HashMap<>();
            if (iPropertyAppUserInfoServiceClient.agreeHoldAudit(projectId, auditId,type) > 0) {
                map.put("state", "2");
            } else {
                map.put("state", "1");
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), map);
        } catch (Exception e) {
            throw new DataAccessException("【物业APP_首页版块_住户审核】住户审核通过失败", e);
        }
    }

}
